package Capa_Modelo;

import java.util.ArrayList;

import java.util.Date;
import java.util.List;

import javax.persistence.Query;

import Capa_Datos.CD_Laboratorio;
import Capa_Datos.CD_RegInstalacion;
import Capa_Datos.CD_Software;

public  class CM_ImplSoftwareDAO extends CM_BaseDao implements CM_SoftwareDAO{
	
	
	public CM_ImplSoftwareDAO(){
		super();
	}

	@Override
	public int RegistrarSwLab(CD_Software software, CD_Laboratorio laboratorio, Date fecha) {
		int indice=1;
		software=buscarSW(software);
		laboratorio=buscarLab(laboratorio);
		CD_RegInstalacion regi =new CD_RegInstalacion();
		regi.setSoftware(software);
		regi.setLaboratorio(laboratorio);
		regi.setFecha(fecha);
		  try{     
				
				em.getTransaction().begin();
			    em.persist(regi);
			    em.flush();
		        em.getTransaction().commit();
		
			}catch(Exception e){
				System.out.print("No existe"+ e.getCause());	
				indice=0;
			}
		return indice;
	}
	
	
	public CD_Software buscarSW(CD_Software software){
        try{     
			
			em.getTransaction().begin();
		Query q=em.createQuery("SELECT s FROM CD_Software s WHERE s.nombre=:nombre AND s.version=:version");
		q.setParameter("nombre",software.getNombre());
		q.setParameter("version",software.getVersion());
	    software=(CD_Software) q.getSingleResult();
	         em.getTransaction().commit();
	
		}catch(Exception e){
			System.out.print("No existe"+ e.getCause());	
			
		}
        
        return software;
	}
	
	
	public CD_Laboratorio buscarLab(CD_Laboratorio laboratorio){
        try{     
			
			em.getTransaction().begin();
		Query q=em.createQuery("SELECT l FROM CD_Laboratorio l WHERE l.idLaboratorio=:numero");
		q.setParameter("numero",laboratorio.getIdLaboratorio());
	    laboratorio=(CD_Laboratorio) q.getSingleResult();
	         em.getTransaction().commit();
	
		}catch(Exception e){
			System.out.print("No existe"+ e.getCause());	
			
		}
        
        return laboratorio;
	}



	@Override
	public int registrar_sw_general(CD_Software software) {
		int indice=1;	
		  try{     
				
				em.getTransaction().begin();
			    em.persist(software);
			    em.flush();
		        em.getTransaction().commit();
		
			}catch(Exception e){
				System.out.print("No guardo"+ e.getCause());	
				indice=0;
			}
		return indice;
	}

	@Override
	public List<CD_Software> retornar_lista_software() {
		List<CD_Software> lista_sw= new ArrayList<CD_Software>();
		try {
		  em.getTransaction().begin();
			Query q=em.createQuery("SELECT r FROM CD_Software r");
		    lista_sw=q.getResultList();
		    em.getTransaction().commit();
		}catch (Exception e) {
			System.out.print("Error software" + e.getCause());
		}
		return lista_sw;
		
	}
	
	@Override
	public CD_Software buscarSwid(int id) {
		
		CD_Software sw=null;
		try{
		    em.getTransaction().begin();
			sw=em.find(CD_Software.class, id);
		    em.getTransaction().commit();
		    
	}catch(Exception e){
			System.out.print("NO HAY-->"+ e.getCause());
		}
		return sw;
	}

	@Override
	public void eliminar_sw(CD_Software selectedSoftware) {
		try{  	
			em.getTransaction().begin();
			em.remove(selectedSoftware);
			em.getTransaction().commit();
		}catch(Exception e){
			em.getTransaction().rollback();
			
			System.out.print("###No se puede eliminar-->"+e.getCause());
			
		}
		 
	}

	@Override
	public void ModificarSoftware(CD_Software selectedSoftware) {
		try{
		//em.getTransaction().begin();
		CD_Software objeto_sw= em.find(CD_Software.class,selectedSoftware.getIdSoftware());
		objeto_sw.setVersion(selectedSoftware.getVersion());
		em.merge(objeto_sw);
		//CD_Software sw_1 = new CD_Software();
		//	sw_1.setNombre("uno");
		//	sw_1.setVersion("1.1");
		//	em.persist(sw_1);
	   
	//	em.getTransaction().commit();
		}catch(Exception e){
			
			System.out.print("Noooo actualizzaaa-->"+ selectedSoftware.getIdSoftware());
			
		}
		
		
	}

	@Override
	public List<CD_RegInstalacion> inicioTabReg() {
		List<CD_RegInstalacion> reg=new ArrayList<CD_RegInstalacion>();

		try {
			  em.getTransaction().begin();
				Query q=em.createQuery("SELECT r FROM CD_RegInstalacion r");
			    reg=q.getResultList();
			    em.getTransaction().commit();
			}catch (Exception e) {
				System.out.print("Error software" + e.getCause());
			}
			return reg;
		
		

	}

	@Override
	public List<Integer> inicioListLabo() {
		List<Integer> lista=new ArrayList<Integer>();
		try {
			  em.getTransaction().begin();
				Query q=em.createQuery("SELECT l.idLaboratorio FROM CD_Laboratorio l");
			    lista=q.getResultList();
			    em.getTransaction().commit();
			}catch (Exception e) {
				System.out.print("Error software" + e.getCause());
			}
			return lista;
	
	}

	@Override
	public int eliminarReg(CD_RegInstalacion reg) {
		int indice=1;
		try{  	
			em.getTransaction().begin();
			em.remove(reg);
		    em.getTransaction().commit();
		}catch(Exception e){
			indice=0;
			System.out.print("Noooo actualizzaaa-->"+reg.getIdInstalacion()+"--"+ e.getCause());
			
		}
		return indice;
	}

	@Override
	public int actualizar(int idInstalacion, CD_Software software,
			CD_Laboratorio laboratorio, Date fecha) {
		CD_RegInstalacion reg=null;
		reg=findReg(idInstalacion);
		software=buscarSW(software);
		laboratorio=buscarLab(laboratorio);
		reg.setSoftware(software);
		reg.setLaboratorio(laboratorio);
		reg.setFecha(fecha);
		int indice=1;
		try{  	
			em.getTransaction().begin();
			em.merge(reg);
		    em.getTransaction().commit();
		}catch(Exception e){
			indice=0;
			System.out.print("Error-->"+ e.getCause());
			
		}
		
		return indice;
	}

	
	public CD_RegInstalacion findReg(int id){
		CD_RegInstalacion reg=null;
		reg=em.find(CD_RegInstalacion.class,id);
		if(reg==null) System.out.print("Error");
		
		return reg;
	}

	@Override
	public List<String> ListaSoftware() {
		List<String> lista_sw= new ArrayList<String>();
		String var="hola";
		try {
		  em.getTransaction().begin();
			Query q=em.createQuery("SELECT CONCAT(r.nombre,CONCAT(' ',r.version)) FROM CD_Software r");
			//q.setParameter(1, var);
		    lista_sw=q.getResultList();
		    em.getTransaction().commit();
		}catch (Exception e) {
			System.out.print("Error software" + e.getCause());
		}
		return lista_sw;
	}

	@Override
	public CD_Software buscarSW(String software) {
		String[] separadas2= new String [2];
		separadas2 = software.split(" ");
		CD_Software softwareS=new CD_Software();
        try{     
        em.getTransaction().begin();
		Query q=em.createQuery("SELECT s FROM CD_Software s WHERE s.nombre=:nombre AND s.version=:version");
		q.setParameter("nombre",separadas2[0] );
		q.setParameter("version",separadas2[1]);
	    softwareS=(CD_Software) q.getSingleResult();
	         em.getTransaction().commit();
	
		}catch(Exception e){
			System.out.print("software"+ e.getLocalizedMessage()+ separadas2[0]+separadas2[1]);	
			
		}
        
        return softwareS;
	
	}
}
